﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Text;
using System.Xml;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using EQT.Model;

namespace EQT.Dal
{
    public partial class DaEmployee
    {
        public DataSet GetEmploreeByDeptID(string deptid)
        {
            string strGetEmploreeByDeptID = "SELECT * FROM EMPLOYEE WHERE Dept_ID=@Dept_ID ";
            try
            {
                DbCommand dbCommand = db.GetSqlStringCommand(strGetEmploreeByDeptID);

                //设置参数:Dept_ID	
                db.AddInParameter(dbCommand, "Dept_ID", DbType.String, deptid);
                return db.ExecuteDataSet(dbCommand);
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return new DataSet();
        }

        public DataSet GetNoUserEmpByDeptID(string deptid)
        {
            string strGetEmploreeByDeptID = "SELECT e.* FROM EMPLOYEE e WHERE e.Dept_ID=@Dept_ID and e.emp_id not in(select emp_id from tuser)";
            try
            {
                DbCommand dbCommand = db.GetSqlStringCommand(strGetEmploreeByDeptID);

                //设置参数:Dept_ID	
                db.AddInParameter(dbCommand, "Dept_ID", DbType.String, deptid);
                return db.ExecuteDataSet(dbCommand);
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return new DataSet();
        }

        public MoEmployee GetEmploreeInfo(string Emp_ID)
        {
            string strGetEmploreeInfo = "SELECT Emp_ID ,Dept_ID ,FullName ,Sex_Code ,IDCard ,BirthDay ,Phone ,Home_Addr ,Rem  from Employee  WHERE Emp_ID=@Emp_ID";
            MoEmployee entity = new MoEmployee();
            try
            {
                DbCommand dbCommand = db.GetSqlStringCommand(strGetEmploreeInfo);

                //设置参数:Emp_Code	
                db.AddInParameter(dbCommand, "Emp_ID", DbType.String, Emp_ID);

                using (IDataReader reader = db.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        entity.EmpId = reader.GetString(0);
                        entity.DeptId = reader.GetString(1);
                        entity.Fullname = reader.GetString(2);
                        entity.SexCode = reader.GetString(3);
                        entity.Idcard = reader.GetString(4);
                        entity.Birthday = reader.GetDateTime(5);
                        entity.Phone = reader.GetString(6);
                        entity.HomeAddr = reader.GetString(7);
                        entity.Rem = reader.GetString(8);
                    }
                }
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return entity;
        }

        /// <summary>
        /// 分页查询。
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public DataSet GetEmploreeRecords(string condition, int PageSize, int PageIndex, out int TotalCount)
        {

            DataSet ds = new DataSet();

            string strGetEmploreeRecords = "select * from V_EmploreeDetail ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetEmploreeRecords = strGetEmploreeRecords + condition;
            }

            try
            {
                ds = DaCommon.GetPaginationData(strGetEmploreeRecords, PageSize, PageIndex, " Emp_ID desc", out TotalCount);
                return ds;
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            TotalCount = 0;
            return new DataSet();
        }

        /// <summary>
        /// 不分页查询。
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public DataSet GetEmploreeRecords(string condition)
        {
            DataSet ds = new DataSet();

            string strGetEmploreeRecords = "select * from V_EmploreeDetail ";

            if (!string.IsNullOrEmpty(condition))
            {
                strGetEmploreeRecords = strGetEmploreeRecords + condition;
            }

            try
            {
                return db.ExecuteDataSet(CommandType.Text, strGetEmploreeRecords);
            }
            catch (DbException ex)
            {
                HandleDbException(ex);
            }
            return new DataSet();
        }

        public MoDept GetEmpDepartment(string Emp_ID)
        {
            DaDept daldept = new DaDept(db);
            MoEmployee modemp = new MoEmployee();
            modemp.EmpId = Emp_ID; 
            if (this.GetEntityEx(ref modemp))
            {
                MoDept mo = new MoDept();
                mo.DeptId = modemp.DeptId;

                if (daldept.GetEntityEx(ref mo))
                {
                    return mo;
                }
                return null;
            }

            return new MoDept();
        }

       

    }
}
